const Record = require("../model/record.model");
const User = require("../model/user.model");
// 使用模糊查询需要先引入Op
const seq = require("sequelize");
const Op = seq.Op;
class RecordService {
  /**
   * 创建
   * @returns {Promise<{code: string, message: string}>}
   * @param record
   */
  async createRecord() {
    return await Record.create();
  }

  /**
   * 更新
   * @param id
   * @param record
   * @returns {Promise<boolean>}
   */
  async updateRecord(id, record) {
    const res = await Record.update(record, { where: { id } });
    return res[0] > 0;
  }

  /**
   * 删除
   * @param id
   * @returns {Promise<boolean>}
   */

  async removeRecord(id) {
    const res = await Record.destroy({ where: { id } });
    return res > 0;
  }

  /**
   * 恢复
   * @param id
   * @returns {Promise<boolean>}
   */

  async restoreRecord(id) {
    const res = await Record.restore({ where: { id } });
    return res > 0;
  }

  async findRecord({ pageNum, pageSize }) {
    const offset = (pageNum - 1) * pageSize;
    const whereOpt1 = {};
    const { count, rows } = await Record.findAndCountAll({
      offset: offset,
      limit: pageSize * 1,
      order: [["createdAt", "DESC"]],
    });

    return {
      pageNum,
      pageSize,
      total: count,
      list: rows,
    };
  }

  async findRecordInfo(id) {
    const whereOpt = {};
    id && Object.assign(whereOpt, { id });

    const res = await Record.findOne({
      where: whereOpt,
    });

    return res ? res.dataValues : null;
  }
}

module.exports = new RecordService();
